import java.util.Comparator;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description Java 对象的比较
 *             优先使用 Comparable 灵活使用 Comparator
 * User: 王杰
 * Date: 2025-06-07
 * Time: 10:15
 */

class Card implements Comparable<Card>{
    public String suit;
    public int rank;

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Card{" +
                "suit='" + suit + '\'' +
                ", rank=" + rank +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Card card)) return false;
        return rank == card.rank && Objects.equals(suit, card.suit);
    }

    @Override
    public int hashCode() {
        return Objects.hash(suit, rank);
    }

    @Override
    public int compareTo(Card o) {
        return this.rank - o.rank;
    }
}

class SuitComparator implements Comparator<Card> {
    @Override
    public int compare(Card o1, Card o2) {
        return o1.suit.compareTo(o2.suit);
    }
}
public class Test {
    public static void main(String[] args) {
        Card card1 = new Card("♥", 13);
        Card card2 = new Card("♠", 5);

        SuitComparator suitComparator = new SuitComparator();
        if(suitComparator.compare(card1, card2) > 0) {
            System.out.println("card1 > card2");
        }else {
            System.out.println("card1 < card2");
        }
    }
    public static void main2(String[] args) {
        Card card1 = new Card("♥", 13);
        Card card2 = new Card("♠", 5);

        if(card1.compareTo(card2) > 0) {
            System.out.println("card1 > card2");
        }else {
            System.out.println("card1 < card2");
        }
    }
    public static void main1(String[] args) {
        Card card1 = new Card("♥", 5);
        Card card2 = new Card("♥", 5);

        System.out.println(card1.equals(card2));
    }
}
